using System;
using System.Collections.Generic;
using Volo.Abp.Domain.Entities.Events;
using Volo.Abp.EventBus.Local;
using TenantManagementService.Tenants;

namespace TenantManagementService.Events
{
    /// <summary>
    /// 租户创建事件
    /// </summary>
    public class TenantCreatedEvent
    {
        /// <summary>
        /// 租户ID
        /// </summary>
        public Guid TenantId { get; set; }

        /// <summary>
        /// 租户编码
        /// </summary>
        public string Code { get; set; } = string.Empty;

        /// <summary>
        /// 租户名称
        /// </summary>
        public string Name { get; set; } = string.Empty;

        /// <summary>
        /// 租户类型
        /// </summary>
        public TenantType Type { get; set; }

        /// <summary>
        /// 租户状态
        /// </summary>
        public TenantStatus Status { get; set; }

        /// <summary>
        /// 管理员邮箱
        /// </summary>
        public string? AdminEmail { get; set; }

        /// <summary>
        /// 试用开始时间
        /// </summary>
        public DateTime? TrialStartTime { get; set; }

        /// <summary>
        /// 试用结束时间
        /// </summary>
        public DateTime? TrialEndTime { get; set; }

        /// <summary>
        /// 最大用户数
        /// </summary>
        public int MaxUserCount { get; set; }

        /// <summary>
        /// 最大存储空间（MB）
        /// </summary>
        public long MaxStorageSize { get; set; }

        /// <summary>
        /// 创建时间
        /// </summary>
        public DateTime CreationTime { get; set; }

        /// <summary>
        /// 创建者
        /// </summary>
        public string? CreatorId { get; set; }

        /// <summary>
        /// 租户配置
        /// </summary>
        public Dictionary<string, object> Configurations { get; set; } = new();

        /// <summary>
        /// 功能开关
        /// </summary>
        public Dictionary<string, bool> FeatureSwitches { get; set; } = new();

        public TenantCreatedEvent()
        {
            CreationTime = DateTime.Now;
        }
    }

    /// <summary>
    /// 租户更新事件
    /// </summary>
    public class TenantUpdatedEvent
    {
        /// <summary>
        /// 租户ID
        /// </summary>
        public Guid TenantId { get; set; }

        /// <summary>
        /// 租户编码
        /// </summary>
        public string Code { get; set; } = string.Empty;

        /// <summary>
        /// 更新时间
        /// </summary>
        public DateTime UpdateTime { get; set; }

        /// <summary>
        /// 更新者
        /// </summary>
        public string? UpdaterId { get; set; }

        /// <summary>
        /// 更新的字段列表
        /// </summary>
        public List<string> UpdatedFields { get; set; } = new();

        /// <summary>
        /// 旧值（可选）
        /// </summary>
        public Dictionary<string, object>? OldValues { get; set; }

        /// <summary>
        /// 新值（可选）
        /// </summary>
        public Dictionary<string, object>? NewValues { get; set; }

        public TenantUpdatedEvent()
        {
            UpdateTime = DateTime.Now;
        }
    }

    /// <summary>
    /// 租户状态变更事件
    /// </summary>
    public class TenantStatusChangedEvent
    {
        /// <summary>
        /// 租户ID
        /// </summary>
        public Guid TenantId { get; set; }

        /// <summary>
        /// 租户编码
        /// </summary>
        public string Code { get; set; } = string.Empty;

        /// <summary>
        /// 租户名称
        /// </summary>
        public string Name { get; set; } = string.Empty;

        /// <summary>
        /// 旧状态
        /// </summary>
        public TenantStatus OldStatus { get; set; }

        /// <summary>
        /// 新状态
        /// </summary>
        public TenantStatus NewStatus { get; set; }

        /// <summary>
        /// 变更时间
        /// </summary>
        public DateTime ChangeTime { get; set; }

        /// <summary>
        /// 变更原因
        /// </summary>
        public string? Reason { get; set; }

        /// <summary>
        /// 变更者
        /// </summary>
        public string? ChangedBy { get; set; }

        public TenantStatusChangedEvent()
        {
            ChangeTime = DateTime.Now;
        }
    }

    /// <summary>
    /// 租户激活事件
    /// </summary>
    public class TenantActivatedEvent
    {
        /// <summary>
        /// 租户ID
        /// </summary>
        public Guid TenantId { get; set; }

        /// <summary>
        /// 租户编码
        /// </summary>
        public string Code { get; set; } = string.Empty;

        /// <summary>
        /// 租户名称
        /// </summary>
        public string Name { get; set; } = string.Empty;

        /// <summary>
        /// 激活时间
        /// </summary>
        public DateTime ActivationTime { get; set; }

        /// <summary>
        /// 激活者
        /// </summary>
        public string? ActivatedBy { get; set; }

        /// <summary>
        /// 是否首次激活
        /// </summary>
        public bool IsFirstActivation { get; set; }

        public TenantActivatedEvent()
        {
            ActivationTime = DateTime.Now;
        }
    }

    /// <summary>
    /// 租户停用事件
    /// </summary>
    public class TenantDeactivatedEvent
    {
        /// <summary>
        /// 租户ID
        /// </summary>
        public Guid TenantId { get; set; }

        /// <summary>
        /// 租户编码
        /// </summary>
        public string Code { get; set; } = string.Empty;

        /// <summary>
        /// 租户名称
        /// </summary>
        public string Name { get; set; } = string.Empty;

        /// <summary>
        /// 停用时间
        /// </summary>
        public DateTime DeactivationTime { get; set; }

        /// <summary>
        /// 停用原因
        /// </summary>
        public string? Reason { get; set; }

        /// <summary>
        /// 停用者
        /// </summary>
        public string? DeactivatedBy { get; set; }

        /// <summary>
        /// 是否强制停用
        /// </summary>
        public bool IsForceDeactivation { get; set; }

        public TenantDeactivatedEvent()
        {
            DeactivationTime = DateTime.Now;
        }
    }

    /// <summary>
    /// 租户订阅续期事件
    /// </summary>
    public class TenantSubscriptionExtendedEvent
    {
        /// <summary>
        /// 租户ID
        /// </summary>
        public Guid TenantId { get; set; }

        /// <summary>
        /// 租户编码
        /// </summary>
        public string Code { get; set; } = string.Empty;

        /// <summary>
        /// 租户名称
        /// </summary>
        public string Name { get; set; } = string.Empty;

        /// <summary>
        /// 延期时长（天）
        /// </summary>
        public int ExtensionDays { get; set; }

        /// <summary>
        /// 新的订阅结束时间
        /// </summary>
        public DateTime NewSubscriptionEndTime { get; set; }

        /// <summary>
        /// 续期时间
        /// </summary>
        public DateTime ExtensionTime { get; set; }

        /// <summary>
        /// 续期者
        /// </summary>
        public string? ExtendedBy { get; set; }

        /// <summary>
        /// 续期次数
        /// </summary>
        public int ExtensionCount { get; set; }

        /// <summary>
        /// 支付信息（可选）
        /// </summary>
        public Dictionary<string, object>? PaymentInfo { get; set; }

        public TenantSubscriptionExtendedEvent()
        {
            ExtensionTime = DateTime.Now;
        }
    }

    /// <summary>
    /// 租户配置变更事件
    /// </summary>
    public class TenantConfigurationChangedEvent
    {
        /// <summary>
        /// 租户ID
        /// </summary>
        public Guid TenantId { get; set; }

        /// <summary>
        /// 租户编码
        /// </summary>
        public string Code { get; set; } = string.Empty;

        /// <summary>
        /// 租户名称
        /// </summary>
        public string Name { get; set; } = string.Empty;

        /// <summary>
        /// 配置键
        /// </summary>
        public string ConfigurationKey { get; set; } = string.Empty;

        /// <summary>
        /// 配置组
        /// </summary>
        public string? ConfigurationGroup { get; set; }

        /// <summary>
        /// 旧配置值
        /// </summary>
        public string? OldValue { get; set; }

        /// <summary>
        /// 新配置值
        /// </summary>
        public string? NewValue { get; set; }

        /// <summary>
        /// 变更时间
        /// </summary>
        public DateTime ChangeTime { get; set; }

        /// <summary>
        /// 变更者
        /// </summary>
        public string? ChangedBy { get; set; }

        /// <summary>
        /// 变更原因
        /// </summary>
        public string? Reason { get; set; }

        public TenantConfigurationChangedEvent()
        {
            ChangeTime = DateTime.Now;
        }
    }

    /// <summary>
    /// 租户使用量超限事件
    /// </summary>
    public class TenantUsageLimitExceededEvent
    {
        /// <summary>
        /// 租户ID
        /// </summary>
        public Guid TenantId { get; set; }

        /// <summary>
        /// 租户编码
        /// </summary>
        public string Code { get; set; } = string.Empty;

        /// <summary>
        /// 租户名称
        /// </summary>
        public string Name { get; set; } = string.Empty;

        /// <summary>
        /// 限制类型
        /// </summary>
        public LimitType LimitType { get; set; }

        /// <summary>
        /// 当前值
        /// </summary>
        public decimal CurrentValue { get; set; }

        /// <summary>
        /// 限制值
        /// </summary>
        public decimal LimitValue { get; set; }

        /// <summary>
        /// 超限百分比
        /// </summary>
        public decimal ExceededPercentage { get; set; }

        /// <summary>
        /// 检测时间
        /// </summary>
        public DateTime DetectionTime { get; set; }

        /// <summary>
        /// 是否已通知管理员
        /// </summary>
        public bool IsAdminNotified { get; set; }

        /// <summary>
        /// 是否已通知租户
        /// </summary>
        public bool IsTenantNotified { get; set; }

        public TenantUsageLimitExceededEvent()
        {
            DetectionTime = DateTime.Now;
        }
    }

    /// <summary>
    /// 租户即将过期事件
    /// </summary>
    public class TenantExpiringSoonEvent
    {
        /// <summary>
        /// 租户ID
        /// </summary>
        public Guid TenantId { get; set; }

        /// <summary>
        /// 租户编码
        /// </summary>
        public string Code { get; set; } = string.Empty;

        /// <summary>
        /// 租户名称
        /// </summary>
        public string Name { get; set; } = string.Empty;

        /// <summary>
        /// 租户类型
        /// </summary>
        public TenantType Type { get; set; }

        /// <summary>
        /// 过期时间
        /// </summary>
        public DateTime ExpirationTime { get; set; }

        /// <summary>
        /// 剩余天数
        /// </summary>
        public int DaysRemaining { get; set; }

        /// <summary>
        /// 通知时间
        /// </summary>
        public DateTime NotificationTime { get; set; }

        /// <summary>
        /// 是否已通知
        /// </summary>
        public bool IsNotified { get; set; }

        /// <summary>
        /// 通知级别
        /// </summary>
        public NotificationLevel NotificationLevel { get; set; }

        public TenantExpiringSoonEvent()
        {
            NotificationTime = DateTime.Now;
        }
    }

    /// <summary>
    /// 限制类型枚举
    /// </summary>
    public enum LimitType
    {
        /// <summary>
        /// 用户数量
        /// </summary>
        UserCount = 1,

        /// <summary>
        /// 存储空间
        /// </summary>
        Storage = 2,

        /// <summary>
        /// API调用次数
        /// </summary>
        ApiCalls = 3,

        /// <summary>
        /// 带宽
        /// </summary>
        Bandwidth = 4
    }

    /// <summary>
    /// 通知级别枚举
    /// </summary>
    public enum NotificationLevel
    {
        /// <summary>
        /// 信息
        /// </summary>
        Info = 1,

        /// <summary>
        /// 警告
        /// </summary>
        Warning = 2,

        /// <summary>
        /// 重要
        /// </summary>
        Important = 3,

        /// <summary>
        /// 紧急
        /// </summary>
        Critical = 4
    }
}